package com.android.pantryshopping;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.pantryshopping.adapter.ShopListCommodityItemAdapter;
import com.android.pantryshopping.bean.Commodity;
import com.android.pantryshopping.bean.ShopList;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class ShopListDetailsActivity extends AppCompatActivity implements ShopListCommodityItemAdapter.onCommodityItemListener{

    private static final String TAG = "DetailsActivity";

    private CommoditySqLiteHelper mCommoditySqLiteHelper;

    private ShopListCommodityItemAdapter mShopListCommodityItemAdapter;

    private ShopList mShopList;
    private ArrayList<Commodity> mCommodities = new ArrayList<>();

    private TextView mTvId;
    private TextView mTvName;
    private Button mBtnEdit;
    private RecyclerView mRvShop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_list_details);

        initView();
        initData();
        initListener();
    }

    private void initListener() {
        findViewById(R.id.btn_back).setOnClickListener(v -> {
            finish();
        });
        mBtnEdit.setOnClickListener(v -> {
            mTvId.setEnabled(true);
            mTvName.setEnabled(true);
        });
        findViewById(R.id.btn_determine).setOnClickListener(v -> {
            ShopList shopList = new ShopList();
            shopList.setId(mTvId.getText().toString().trim());
            shopList.setCheckList(mShopList.getCheckList());
            shopList.setCommodityList(mShopList.getCommodityList());
            shopList.setName(mTvName.getText().toString().trim());
            mCommoditySqLiteHelper.updateShopListData(shopList);
            finish();
        });
    }

    private void initData() {
        int position = getIntent().getIntExtra("position", 0);

        mCommoditySqLiteHelper = new CommoditySqLiteHelper(this,"Commodity_db",null,1);
        List<ShopList> shopListList = mCommoditySqLiteHelper.queryAllShopListData();
        List<Commodity> commodities = mCommoditySqLiteHelper.queryAllCommodityData();
        mShopList = shopListList.get(position);
        Gson gson = new Gson();
        Type type = new TypeToken<ArrayList<String>>() {
        }.getType();
        ArrayList<String> mCheckList = gson.fromJson(mShopList.getCheckList(), type);
        if (mCheckList != null) {
            ArrayList<String> conversionSetList = removeDuplicate(mCheckList);
            for (int i = 0; i < conversionSetList.size(); i++) {
                Commodity commodity = commodities.get(i);
                if (mCheckList.contains(String.valueOf(i))) {
                    mCommodities.add(commodity);
                }
            }
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        mRvShop.setLayoutManager(layoutManager);
        mShopListCommodityItemAdapter = new ShopListCommodityItemAdapter(this);
        mShopListCommodityItemAdapter.setOnCommodityItemListener(this);
        mShopListCommodityItemAdapter.setCommodities(mCommodities);
        mRvShop.setAdapter(mShopListCommodityItemAdapter);

        mTvId.setText(mShopList.getId());
        mTvName.setText(mShopList.getName());
    }

    private ArrayList<String> removeDuplicate(ArrayList<String> mCheckList) {
        Set set = new LinkedHashSet<String>(mCheckList);
        mCheckList.clear();
        mCheckList.addAll(set);
        return mCheckList;
    }

    private void initView() {
        mTvId = findViewById(R.id.tv_id);
        mTvName = findViewById(R.id.tv_name);
        mBtnEdit = findViewById(R.id.btn_edit);
        mRvShop = findViewById(R.id.rv_shop);
    }

    @Override
    public void onCommodityClick(int position) {
        Intent intent = new Intent(this,DetailsActivity.class);
        intent.putExtra("position",position);
        startActivity(intent);
    }
}